package com.ymkyve.complex.jms;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Project: JmsDemo
 * 
 * File Created at Aug 21, 2009
 * $Id$
 * 
 * Copyright 2008 Alibaba.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */

/**
 * @author ding.lid
 */
public class SimpleReceiver {
    private static final Log logger = LogFactory.getLog(SimpleReceiver.class);

    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
                "tcp://localhost:61616");
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createQueue("JMS.DEMO");
        //Destination destination = session.createTopic("JMS.DEMO");

        MessageConsumer consumer = session.createConsumer(destination);

        // 接收一条消息
        Message message = consumer.receive(500);

        if (message != null)
            logger.info(message);
        else {
            logger.warn("fail to receive a message!");
        }

        session.close();
        connection.close();
    }
}
